package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysFinanceType;
import com.settlement.system.mapper.SysFinanceTypeMapper;
import com.settlement.system.service.SysFinanceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysFinanceTypeQuery;
import com.settlement.system.model.form.SysFinanceTypeForm;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.SysFinanceTypeImportVo;

/**
 * <p>
 * 资金项目类别 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-29
 */
@Service
public class SysFinanceTypeServiceImpl extends ServiceImpl<SysFinanceTypeMapper, SysFinanceType> implements SysFinanceTypeService {

    @Override
    public Page<SysFinanceType> getPage(SysFinanceTypeQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysFinanceType> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysFinanceType(SysFinanceTypeForm sysFinanceTypeForm) {
         SysFinanceType sysFinanceType = new SysFinanceType();
         BeanUtils.copyProperties(sysFinanceTypeForm, sysFinanceType);
         sysFinanceType.setCreateTime(null);
         return this.saveOrUpdate(sysFinanceType);
     }

     @Override
     public SysFinanceTypeForm getSysFinanceTypeFormData(Long id) {
         SysFinanceType sysFinanceType = this.getById(id);
         SysFinanceTypeForm sysFinanceTypeForm = new SysFinanceTypeForm();
         BeanUtils.copyProperties(sysFinanceType, sysFinanceTypeForm);
         return sysFinanceTypeForm;
     }

     @Override
     public boolean deleteSysFinanceType(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysFinanceTypeImportVo> voList)  {
        List<SysFinanceType> sysFinanceTypeList = new ArrayList<>();
        for (SysFinanceTypeImportVo importVo : voList) {
            SysFinanceType sysFinanceType = new SysFinanceType();
            BeanUtils.copyProperties(importVo, sysFinanceType);
            sysFinanceTypeList.add(sysFinanceType);
        }
        this.saveOrUpdateBatch(sysFinanceTypeList);
    }

    @Override
    public List<SysFinanceTypeImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysFinanceType> sysFinanceTypeList = this.getBaseMapper().selectBatchIds(ids);
        List<SysFinanceTypeImportVo> voList = new ArrayList<>();
        for (SysFinanceType sysFinanceType : sysFinanceTypeList) {
            SysFinanceTypeImportVo vo = new SysFinanceTypeImportVo();
            BeanUtils.copyProperties(sysFinanceType, vo);
            voList.add(vo);
        }
        return voList;
    }
}
